package com.yiwei.hdb.activity;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.yiwei.hdb.R;
import com.yiwei.hdb.adapter.BankCardListAdapter;
import com.yiwei.hdb.api.Api;
import com.yiwei.hdb.event.SelectBankWithdrawEvent;
import com.yiwei.hdb.model.BankCardListModel;
import com.yiwei.hdb.util.Network;
import com.yiwei.hdb.util.RxLifeUtil;
import com.yiwei.hdb.util.ToastUtil;
import com.yiwei.hdb.util.Util;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2018/6/10.
 */

public class V3BankMoreListActivity extends BaseActivity {
    @BindView(R.id.left_img)
    ImageButton leftImg;
    @BindView(R.id.title_text)
    TextView titleText;
    @BindView(R.id.btn_add)
    ImageView btnAdd;
    @BindView(R.id.list)
    RecyclerView list;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;
    @BindView(R.id.img_bank_bg)
    ImageView imgBankBg;
    @BindView(R.id.btn_add_empty)
    SuperButton btnAddEmpty;
    @BindView(R.id.layout_without_bank)
    RelativeLayout layoutWithoutBank;
    private Unbinder unbinder;
    private BankCardListAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bank_list_more);
        initBar();
        unbinder = ButterKnife.bind(this);
        init();
    }

    private void init() {
        boolean isClick = getIntent().getBooleanExtra("is_click", false);
        adapter = new BankCardListAdapter(this);
        adapter.setShareClickListener(new BankCardListAdapter.OnShareClickListener() {
            @Override
            public void onClick(BankCardListModel.DataBean item) {
                if (isClick) {
                    EventBus.getDefault().post(new SelectBankWithdrawEvent(item));
                    finish();
                }
            }
        });
        list.setAdapter(adapter);
        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getBankList();
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        getBankList();
    }

    private void getBankList() {
        if (!Network.checkNetwork(this)) {
            ToastUtil.makeText(this, "网络连接失败");
            return;
        }
        refresh.setRefreshing(true);
        Api.api_service.getBankcardList().compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        refresh.setRefreshing(false);
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    adapter.update(result.data);
                    if (adapter.getItemCount() == 0) {
                        layoutWithoutBank.setVisibility(View.VISIBLE);
                    } else {
                        layoutWithoutBank.setVisibility(View.GONE);
                    }
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
        unbinder = null;
    }

    @OnClick({R.id.btn_add_empty, R.id.left_img, R.id.btn_add})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_add_empty:
                Util.startActivity(this, BankIdentityActivity.class);
                break;
            case R.id.left_img:
                finish();
                break;
            case R.id.btn_add:
                Util.startActivity(this, BankIdentityActivity.class);
                break;
        }
    }
}
